package facade

import "fmt"

// 以用户注册流程为例演示Facade模式
type UserValidator struct {
}

func (u *UserValidator) Validate(name, email string) error {
	// 验证用户信息
	fmt.Printf("验证用户信息：%s, %s\n", name, email)
	return nil
}

// 用户信息存储
type UserRepository struct {
}

func (u *UserRepository) CreateUser(name, email string) error {
	// 创建用户
	fmt.Printf("创建用户：%s, %s\n", name, email)
	return nil
}

// 发送邮件
type EmailService struct {
}

func (r *EmailService) SendEmail(email string) {
	fmt.Printf("发送邮件：%s\n", email)
}

type UserRegistrationFacade struct {
	validator    *UserValidator
	repository   *UserRepository
	emailService *EmailService
}

func NewUserRegistrationFacade() *UserRegistrationFacade {
	return &UserRegistrationFacade{
		validator:    &UserValidator{},
		repository:   &UserRepository{},
		emailService: &EmailService{},
	}
}

func (r *UserRegistrationFacade) RegisterUser(name, email string) (err error) {
	if err = r.validator.Validate(name, email); err != nil {
		fmt.Printf("验证失败：%v\n", err)
		return
	}
	if err = r.repository.CreateUser(name, email); err != nil {
		fmt.Printf("创建用户失败：%v\n", err)
		return
	}
	// 执行后续流程
	go func() {
		r.emailService.SendEmail(email)
	}()
	return nil
}
